﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _02_WebControl : System.Web.UI.Page
{
    //DropDownList数据源集合
    private List<MyItem> myItems = new List<MyItem>();

    protected void Page_Init(object sender, EventArgs e)
    {
        CreateData();
    }

        protected void Page_Load(object sender, EventArgs e)
    {
        //定义下拉列表显示项相关属性
        DdlProvnice.DataTextField = "ItemText";
        DdlProvnice.DataValueField = "ItemValue";
        DdlCity.DataTextField = "ItemText";
        DdlCity.DataValueField = "ItemValue";
        if (!isPostBcak)
        {
            //委托 可以直接用“=”赋值；事件必须用+=，-=
            Predicate<Myitem> _p = _f;
            DdlProvnice.DataSource = myItems.FindAll(_p);
            DdlProvnice.DataBind();
            Ddlprovnice.SelectedIndex = 0;

            //匿名函数
            DdlCity.DataSource = myItems.FindAll(delegate (Myitem myitem) {
            return myitem.ItemParent == Ddlprovnice.SelectedValue;
                )};
            DdlCity.DataBind();
        

        }

    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        
    }

    protected void Ddlprovnice_SelectedIndexChanged(object sender, EventArgs e)
    {
        DdlCity.DataSource = myItems.FindAll(myitem=>myitem.itemParent == Ddlprovnice.SelectedValue);
        DdlCity.DataBind();

    }
    private void CreateData()
    {
        //0表示没有上级
        myItems.Add(new MyItem("bj", "北京市", "0"));
        myItems.Add(new MyItem("sh", "上海市", "0"));
        myItems.Add(new MyItem("jx", "江西市", "0"));
        myItems.Add(new MyItem("gd", "广东市", "0"));
        myItems.Add(new MyItem("hb", "湖北市", "0"));
        //bj表示上级菜单使bj
        myItems.Add(new MyItem("haidian", "海淀区", "bj"));
        myItems.Add(new MyItem("xujiahui", "徐家汇", "sh"));
        myItems.Add(new MyItem("abc", "阿百川", "jx"));
        myItems.Add(new MyItem("def", "得分", "gd"));
        myItems.Add(new MyItem("hij", "hi就", "hb"));
    }

    private Bool _f(Myitem item)
    {
        return item.ItemParent == "0";
    }
}
class Myitem
{
    private string itemValue;
    private string itemText;
    private string itemParent;

    public Myitem(string itemValue, string itemText, string itemParent)
    {
        this.itemValue = itemValue;
        this.itemText = itemText;
        this.itemParent = itemParent;
    }

    public string ItemValue { get => itemValue; set => itemValue = value; }
    public string ItemText { get => itemText; set => itemText = value; }
    public string ItemParent { get => itemParent; set => itemParent = value; }
}